//################################################################################################################################//
//##                                                                                                                            ##//    
//## This control is implemented for "Refer to a friend" Section.                                                                  ##//
//##                                                                                                                            ##//
//## Author   : Manish Prajapati                                                                                                ##//
//## Dated    : 22-03-2006                                                                                                      ##//
//##                                                                                                                            ##//    
//################################################################################################################################//
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using SiteManager;
using System.Net.Mail;
public partial class includes_tellaFriend : System.Web.UI.UserControl
{
    Manager objmanager = new Manager(true,"ConnectionString");
    /// <summary>
    /// Check User Login and Show User Details
    /// </summary>

    protected void Page_Load(object sender, EventArgs e)
    {
        localFunction lFunction = new localFunction();
        ddlTypeMessage.Attributes.Add("onchange", "AutoMessage('" + ddlTypeMessage.ClientID.ToString() + "','" + txtMessage.ClientID + "')");
//        if (Session["userType"].ToString() == "SiteUser")
//        {
//// check if user logged in
//            if (!lFunction.checkUserLogin(false))
//            {
//                Response.Redirect(Session["strHttp"] + "default.aspx");
//            }
//            else
//            {
//                lblName.Text = Session["LoginName"].ToString();
//                lblEmail.Text = Session["UserEmail"].ToString();
//                multiView.Visible = true;
//            }
//        }
//        else
//        {
//            this.Visible = false;
//        }
    }
    /// <summary>
    /// Send the message to friend
    /// </summary>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblNoEmail.Visible = false;
        Validation IsValidate = new Validation();
        TextBox txtEMail;
        //StringBuilder strMessage = new StringBuilder(string.Empty);
        StringBuilder strMessage = new StringBuilder();

        IsValidate.AddValidation(txtUname.Text.Trim(), Validation.ValidationCommand.required, "Please enter name.", "");
        IsValidate.AddValidation(txtuEmail.Text.Trim(), Validation.ValidationCommand.maxlen, "Email maximum length is only 50 characters.", "50");
        IsValidate.AddValidation(txtuEmail.Text.Trim(), Validation.ValidationCommand.email, "Please enter a valid email Id.", "");
        IsValidate.AddValidation(txtuEmail.Text.Trim(), Validation.ValidationCommand.required, "Please enter email Id.", "");

        for (int i = 1; i <= 5; i++)
        {
            // check validations
            txtEMail = (TextBox)multiView.FindControl("txtEmail" + i);
            IsValidate.AddValidation(txtEMail.Text.Trim(), Validation.ValidationCommand.maxlen, "Email maximum length is only 50 characters for Friend " + i, "50");
            IsValidate.AddValidation(txtEMail.Text.Trim(), Validation.ValidationCommand.email, "Please enter a valid email Id for Friend " + i + ".", "");
        }
        if (IsValidate.CheckError() == false)
        {
            errorlist.DataSource = IsValidate.ErrList;
            errorlist.DataBind();
            errorlist.Visible = true;
        }
        else
        {
            errorlist.Visible = false;
            if (txtEmail1.Text.Trim().Length > 0 || txtEmail2.Text.Trim().Length > 0 || txtEmail3.Text.Trim().Length > 0 || txtEmail4.Text.Trim().Length > 0 || txtEmail5.Text.Trim().Length > 0)
            {
                //strMessage = "Message Type : - " + ddlTypeMessage.SelectedItem.Text.ToString() + "<BR>" + txtMessage.Text;

                //K: The above line is replaced by the line below as per client's request.
                //25 October 2006
                strMessage.Append(txtMessage.Text);
                strMessage.Append("<p>");
                strMessage.Append(txtUname.Text);
                string strMessageStatus = "";
                for (int i = 1; i <= 5; i++)
                {
                    txtEMail = (TextBox)multiView.FindControl("txtEmail" + i);
                    if (txtEMail.Text != "")
                    {
                        // send mail
                        //K: This code is replaced to change the 'from mail' to 'contact@findadoc.com'
                        //objmanager.sendMail(strMessage, txtEMail.Text, txtUname.Text, txtSubject.Text, txtuEmail.Text, "");
                        //objmanager.sendMail(Convert.ToString(strMessage), txtEMail.Text, txtUname.Text, txtSubject.Text, "contact@findadoc.com", "");
                         strMessageStatus = SendEmailToDoctorDentist(Convert.ToString(strMessage), txtEMail.Text, txtUname.Text, txtSubject.Text, "contact@findadoc.com");
                     
                    }
                }
                if (strMessageStatus == "Send Successfully")
                {
                    multiView.SetActiveView(View2);
                    // ClientScript.RegisterStartupScript(typeof(Page), "MessagePopUp", "alert('Email Sent Successfully');", true);
                }
                else
                {
                    lblMessage.Visible = true;
                    lblMessage.Text = "Email can not be sent.Please try again.";
                    lblMessage.ForeColor = System.Drawing.Color.Red;
                }
               
            }
            else
            {
                lblNoEmail.Text = "<FONT color=red>Please enter atleast one email Id .</FONT>";
                lblNoEmail.Visible = true;
            }
        }
    }

    protected string SendEmailToDoctorDentist(string strBody, string strTo, string fromName, string strSubject, string strFrom)
    {
        SmtpClient smtpClient = new SmtpClient();
        MailMessage message = new MailMessage();
        try
        {
            message.From = new MailAddress(strFrom);
            message.Subject = strSubject;
            message.Body = strBody;
            message.IsBodyHtml = true;
            // smtpClient.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
            smtpClient.Host = "smtp.1and1.com"; //Or Your SMTP Server Address
            smtpClient.Credentials = new System.Net.NetworkCredential("docfriend@findadoc.com", "rateddoc");
            message.To.Add(strTo);
            //smtpClient.EnableSsl = true;
            smtpClient.Send(message);
            message = null;
            smtpClient = null;
            return "Send Successfully";
            //            The Host address is
            //www.1and1.com

            //username and password for the Domain Web Mail is
            //docfriend@findadoc.com
            //rateddoc


        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
}
